package com.xzkydz.sensor.sensor.base;


import com.xzkydz.sensor.utils.CalculationUtils;
import com.xzkydz.sensor.utils.RandomUtils;

import java.util.Arrays;

/**
 * 传感器对象基类
 */
public class BaseSensorData {

    /**
     * 传感器单条数据包
     */
    public byte[] bRec;
    /**
     * 解析数据包的时间（并非传感器实际采集数据的时间）
     */
    public Long time;
    /**
     * 目的地址（16进制字符串）
     */
    public String addressVel;
    /**
     * 生产批号（16进制字符串）
     */
    public String batchNumVel;
    /**
     * 客户编码（16进制字符串）
     */
    public String clientCodeVel;
    /**
     * 仪器编码（16进制字符串）
     */
    public String deviceCodeVel;
    /**
     * 传感器类型（16进制字符串）
     */
    public String sensorTypeVel;
    /**
     * 传感器编号 （16进制字符串）
     */
    public int sensorNumVel;
    /**
     * 数据包序号
     */
    public int dataIndex;
    /**
     * 应用
     */
    public String dataType;
    /**
     * 传感器电量
     */
    public int powerVel;
    /**
     * 传感器信号强度
     */
    public int signalVel;


    public BaseSensorData() {

    }

    /**
     * 解析传感器
     *
     * @param recData 数据包
     * @return
     */
    protected void calculate(byte[] recData) {
        if (recData == null || recData.length < 16) {
            return;
        }
        this.bRec = recData;
        this.time = System.currentTimeMillis();
        this.addressVel = CalculationUtils.twoBytesToHexStr(recData[3], recData[4]);
        this.batchNumVel = CalculationUtils.twoBytesToHexStr(recData[5], recData[6]);
        this.clientCodeVel = CalculationUtils.byte2Hex(recData[7]);
        this.deviceCodeVel = CalculationUtils.byte2Hex(recData[8]);
        this.sensorTypeVel = CalculationUtils.byte2Hex(recData[9]);
        this.sensorNumVel = CalculationUtils.oneByteToIntNoSign(recData[10]);
        this.dataIndex = CalculationUtils.twoByteToIntNoSign(recData[11], recData[12]);
        this.dataType = CalculationUtils.byte2Hex(recData[13]);
        this.powerVel = CalculationUtils.twoByteToIntNoSign(bRec[recData.length - 4], bRec[recData.length - 3]);
        this.signalVel = CalculationUtils.oneByteToIntNoSign(recData[recData.length - 2]);
    }

    /**
     * 模拟传感器数据数据
     *
     * @return
     */
    protected void simulation(int sensorNum) {
        sensorNumVel = sensorNum;
        time = System.currentTimeMillis();
        powerVel = RandomUtils.getInstance().randomInt(300, 120);
        signalVel = RandomUtils.getInstance().randomInt(0, 200);
        dataType = "0" + RandomUtils.getInstance().randomInt(0, 9);
    }

    /**
     * 传参并解析返回实体类
     *
     * @param recData
     * @param <T>
     * @return
     */
    public <T extends BaseSensorData> T caculateFunc(byte[] recData) {
        calculate(recData);
        return (T) this;
    }

    /**
     * 返回随机生成测试数据实体类
     *
     * @param sensorNum
     * @param <T>
     * @return
     */
    public <T extends BaseSensorData> T simulationFunc(int sensorNum) {
        simulation(sensorNum);
        return (T) this;
    }

    @Override
    public String toString() {
        return "BaseSensorData{" +
                "bRec=" + Arrays.toString(bRec) +
                ", time=" + time +
                ", addressVel='" + addressVel + '\'' +
                ", batchNumVel='" + batchNumVel + '\'' +
                ", clientCodeVel='" + clientCodeVel + '\'' +
                ", deviceCodeVel='" + deviceCodeVel + '\'' +
                ", sensorTypeVel='" + sensorTypeVel + '\'' +
                ", sensorNumVel=" + sensorNumVel +
                ", dataIndex=" + dataIndex +
                ", dataType='" + dataType + '\'' +
                ", powerVel=" + powerVel +
                ", signalVel=" + signalVel +
                '}';
    }
}
